<?php


add_filter('attachment_fields_to_save', 'mmww_attachment_fields_to_save' ,10, 2);
function mmww_attachment_fields_to_save ($post, $attachment) {
	//echo __FUNCTION__;
	return $post;
}

global $mmww_saved_types;
$mmww_saved_types = array();
add_filter('wp_read_image_metadata_types', 'mmww_wp_read_image_metadata_types');
/**
 * adjust list of image types to handle.
 * @link http://www.php.net/manual/en/image.constants.php
 * @param array $types array of image types
 * @return array of types
 */
function mmww_wp_read_image_metadata_types ($types) {
	global $mmww_saved_types;
	$mmww_saved_types = $types;
	return $types;
}

/**
 * combine two metadata arrays key by key, overwriting the old with the new
 * @param string array $new
 * @param string array $old
 * @return string array of merged metadata
 */
function mmww_merge($new, $old) {
	foreach ($new as $k => $v) {
		if ( ! empty ($v) && strlen ($v) > 0){
			$old[$k] = $v;
		}
	}
	return $old;
}

/**
 * we need to be able to retrieve post IDs and MIME types in the
 * read_image_metadata filter context.
 */
global $mmww_mime_by_file;
$mmww_mime_by_file = array();
global $mmww_post_by_file;
$mmww_post_by_file = array();


global $mmww_saved_meta;
$mmww_saved_meta = array ();
add_filter('wp_read_image_metadata', 'mmww_wp_read_image_metadata',10,3);

/**
 * filter to extend the stuff in wp_admin/includes/image.php
 *        and store the metadata in the right place.
 *        This function handles xmp, iptc, exif, and id3v2
 *        and so copes pretty well with pdf, mp3, etc.
 * @param array $meta  associative array containing pre-loaded metadata
 * @param string $file file name
 * @param string $sourceImageType MIME type
 * @return bool|array False on failure. Image metadata array on success.
 */
function mmww_wp_read_image_metadata ($meta, $file, $sourceImageType) {
	global $mmww_saved_types;
	global $mmww_saved_meta;
	global $mmww_mime_by_file;
	global $mmww_post_by_file;
	
	
	if ( ! file_exists( $file ) ) {
		return $meta;
	}
	
	$mime = $mmww_mime_by_file[$file];
	$postID = $mmww_post_by_file[$file];
	
	/* try to get the metadata from the various sources */
	require_once 'xmp.php';
	require_once 'iptc.php';
	require_once 'exif.php';
	require_once 'id3.php';
	
	/* merge it up  -- later merges potentially overwrite earlier ones*/
	
	$id3meta = mmww_get_id3_metadata ($file);
	$meta = mmww_merge($id3meta, $meta);
	$id3xmpmeta = mmww_get_xmp_audio_metadata ($file);
	$meta = mmww_merge($id3xmpmeta, $meta);
	$exifmeta = mmww_get_exif_metadata ($file);
	$meta = mmww_merge($exifmeta, $meta);
	$iptcmeta = mmww_get_iptc_metadata ($file);
	$meta = mmww_merge($iptcmeta, $meta);
	$xmpmeta =  mmww_get_xmp_metadata ($file);
	$meta = mmww_merge($xmpmeta, $meta);

	/* fix up the copyright statement to present in a compliant way */
	if ( ! empty ($meta['copyright']) ) {
		$meta['copyright'] = __( "Copyright &#169; ", 'mmww' ) . $meta['copyright'];
	}
	
	$mmww_saved_meta = $meta;
	return $meta;
}

add_filter('wp_get_attachment_metadata', 'mmww_wp_get_attachment_metadata',10,2);
function mmww_wp_get_attachment_metadata ($meta,$postId) {
	//echo __FUNCTION__;
	return $meta;
}

add_filter('wp_generate_attachment_metadata', 'mmww_wp_generate_attachment_metadata',10,2);
function mmww_wp_generate_attachment_metadata ($metadata, $attachment_id) {
	//echo __FUNCTION__;
	return $metadata;
}



/**
 *  Edit fields in the media upload editor
 * @param associative array of fields $file
 * @param associative array describing $post
 * @return updated array of fields
 */
function mmww_fields_to_edit($fields,$post) {

	$file = "../" . str_replace(get_bloginfo('wpurl'). "/", "", $fields['image_url']['value']);	
	$mime = get_post_mime_type($post->ID);

	/*
	 * we need to be able to retrieve post IDs and MIME types in the
	 * read_image_metadata filter context, so save them.
	 */
	global $mmww_mime_by_file;
	$mmww_mime_by_file[$file] = $mime;
	global $mmww_post_by_file;
	$mmww_post_by_file[$file] = $post->ID;
	$metadata_refreshed = false;

	/* do we have metadata already stored for this one? */
	$jsonmeta = get_post_meta($post->ID,MMWW_POSTMETA_KEY,true);
	if (! empty ($jsonmeta)) {
		$meta = json_decode($jsonmeta, true);
		$metadata_refreshed = false;
	}
	else {
		/* get the metadata from the media file. */
		$meta = wp_read_image_metadata( $file );
		$jsonmeta = json_encode ($meta);
		add_post_meta($post->ID,MMWW_POSTMETA_KEY,$jsonmeta);
		$metadata_refreshed = true;
	}

	/* do we have refreshed metadata? if so, update the fields */
	if ($metadata_refreshed) {
	
		$form_fields['post_title']['required'] = true;
		
		$fields['image_alt']['value'] = implode(' ', array($meta['title'],$meta['credit']));

		$string .= '<table><tr><td>tag</td><td>value</td></tr>' . "\n";
		foreach ($meta as $tag => $value) {
			$string .= '<tr><td>' . $tag . '</td><td>' . $value .'</td></tr>' . "\n";
		}
		$string .= '</table>' . "\n";
		
		$fields['post_content']['value'] = $string;
	}

	return $fields;
}

?>